*****************************************************************
* 'Crash for cash' replication file (EJPE-22-00212)
*****************************************************************

* Data and variables
*****************************************************************

  use "EJPE_replication", clear

  qui xi:reg IMFnn i.regid i.year i.cid
  sort cid year 
  
* Controls  
  
  qui g lngdppc=ln(wdi_gdpcapcon2010)
  qui g lnpop=ln(pop_PW)
  qui g lncpi=asinh(infl_cpi_WDI)
  qui g dcpi=d.infl_cpi_WDI
  qui g dlncpi=asinh(dcpi)
  qui g lndebt=ln(extdebt_WDI)
  qui g chga=democracy
  qui egen demo_mean=mean(democracy), by(cid)
  replace chga=round(demo_mean) if democracy==.
  qui g lninr_spread=asinh(inr_spread)
  qui g coupi=coup_any
  replace coupi=0 if coup_any==.
  qui g lnref=ln(1+refasy)
  qui g lnliq=ln(imf_liq)

* Independent variables  
  
  replace pwt_rgdp=1.042*(l.pwt_rgdp) if year==2018
  gen xbis_flow=(1e0)*bis_flow/pwt_rgdp
  gen xbis_safeh=(1e0)*bis_safeh/pwt_rgdp
  gen xbis_taxh=(1e0)*bis_taxh/pwt_rgdp
  gen xbis_anders=(1e0)*bis_anders/pwt_rgdp
  g realmis_gdp=realmisinv/(realkf_PERI/realkf_gdp) 
  
  local j=1
  foreach var in flow taxh safeh anders{
	qui egen av=mean(xbis_`var'), by(cid)
	qui g xbis_trend`j'=xbis_`var'-av
	lab var xbis_trend`j' "De-trended BIS/GDP flow [`j']"
	qui drop av
	local j=`j'+1
	}
	rename xbis_trend4 xbis_trend5
	rename xbis_trend3 xbis_trend4
	rename xbis_trend2 xbis_trend3
	
  foreach var in realkf realmis{
	qui egen av=mean(`var'_gdp), by(cid)
	qui g x`var'_trend=`var'_gdp-av
	drop av
  }
    
  g exp=exp_WDI*gdp_WDI/100
  egen fuelres_WDI=rowmax(natres ex_fuel)
  g expnet=exp_WDI-fuelres_WDI
  replace expnet=expnet*gdp_WDI/100
  
  qui xtreg bis_flow exp _Iyear*, fe 
    predict y1, xb
	g bis_dev1=bis_flow-y1
	replace bis_dev1=asinh(bis_dev1)
	g bis_plus1=bis_dev1>0 
	replace bis_plus1=. if bis_dev1==.
 
  global Z lngdppc dlncpi reserves_WDI crisis_LV 
  global Z1 $Z chga coupi s_unga3g7 lnref
  
* Sample of countries
  
  egen Nimf=sum(IMFnn), by(cid)
  drop if incid==1 & (Nimf==0 | Nimf==.)
  
* Predictor means for CRE probit

  foreach z in lngdppc dlncpi reserves_WDI crisis chga fh_inv p_ipolity coupi s_unga lnref{
    qui egen _m_`z'=mean(`z'), by(cid)
  }
  global m_z  _m_lngdppc _m_dlncpi _m_reserves_WDI _m_crisis 
  global m_z1 $m_z _m_chga _m_coupi _m_s_unga _m_lnref
  
  foreach x in xbis_trend1 xbis_trend3 xbis_trend4 xbis_trend5 bis_plus1 bis_dev1 realkf_gdp{
    qui egen _m_`x'=mean(`x'), by(cid)
	}  
  
  
* Main analysis
*****************************************************************

* Capital flight and IMF program participation

  mat Diag1=J(2,3,0)  
  qui meprobit IMFnn xbis_trend3 l.IMFnn xbis_flow _m_xbis_trend3 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store A11
   mat Diag1[1,1]=e(N)
   qui probit IMFnn l.IMFnn xbis_trend3 xbis_flow _m_xbis_trend3 _Iyear*
   mat Diag1[2,1]=e(r2_p)
  qui meprobit IMFnn xbis_trend3 l.IMFnn $Z xbis_flow $m_z _m_xbis_trend3 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store A12
   mat Diag1[1,2]=e(N)
   qui probit IMFnn xbis_trend3 l.IMFnn $Z xbis_flow $m_z _m_xbis_trend3 _Iyear* 
   mat Diag1[2,2]=e(r2_p)
  qui meprobit IMFnn xbis_trend3 l.IMFnn $Z1 xbis_flow $m_z1 _m_xbis_trend3 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store A13 
   mat Diag1[1,3]=e(N)
   qui probit IMFnn xbis_trend3 l.IMFnn xbis_flow $Z1 $m_z _m_xbis_trend3 _Iyear* 
   mat Diag1[2,3]=e(r2_p)
  
  ** Table 1 **    
  estout A1*, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  
  mat li Diag1
  
  qui g lIMFnn=l.IMFnn
  qui probit IMFnn xbis_trend3 lIMFnn $Z xbis_flow $m_z _m_xbis_trend3 _Iyear_2001-_Iyear_2018, vce(cl cid) 
  mfx, at(xbis_trend3=0)
  ** 27.9% **
  mfx, at(xbis_trend3=3.85)
  ** 32.8% **

  qui probit IMFnn xbis_trend3 lIMFnn $Z1 xbis_flow $m_z1 _m_xbis_trend3 _Iyear_2001-_Iyear_2018, vce(cl cid) 
  mfx, at(xbis_trend3=0)
  ** 27.4% **
  mfx, at(xbis_trend3=3.85)
  ** 31.9% **

  
* Capital flight, program participation, and IMF conditionality 
  
  qui g ivlang=imfav*lnliq
  cmp setup 
  mat Cdiag=J(5,3,0)
 
  qui cmp (ba2tot=xbis_trend3 _Icid* _Iyear*) (IMFnn=ivlang imfav lnliq _Icid* _Iyear*) if year>=2000, indicators(IMFnn $cmp_cont) ro cl(cid) iterate(35)
   est store B11
   mat Cdiag[1,1]=e(N1)
   mat Cdiag[3,1]=e(N2)
   qui test [IMFnn]ivlang
   mat Cdiag[5,1]=r(chi2)
   qui reg ba2tot xbis_trend3 _Icid* _Iyear* 
   mat Cdiag[2,1]=e(r2_a)
   qui reg IMFnn ivlang imfav lnliq _Icid* _Iyear* if year>=2000
   mat Cdiag[4,1]=e(r2_a)
  qui cmp (ba2tot=xbis_trend3 $Z _Icid* _Iyear*) (IMFnn=ivlang lnliq $Z _Icid* _Iyear*) if year>=2000, indicators(IMFnn $cmp_cont) ro cl(cid) iterate(35)
   est store B12
   mat Cdiag[1,2]=e(N1)
   mat Cdiag[3,2]=e(N2)
   qui test [IMFnn]ivlang
   mat Cdiag[5,2]=r(chi2)
   qui reg ba2tot xbis_trend3 $Z _Icid* _Iyear* 
   mat Cdiag[2,2]=e(r2_a)
   qui reg IMFnn ivlang imfav $Z lnliq _Icid* _Iyear* if year>=2000
   mat Cdiag[4,2]=e(r2_a)
  qui cmp (ba2tot=xbis_trend3 $Z1 _Icid* _Iyear*) (IMFnn=ivlang imfav lnliq $Z1 _Icid* _Iyear*) if year>=2000, indicators(IMFnn $cmp_cont) ro cl(cid) iterate(35)
   est store B13 
   mat Cdiag[1,3]=e(N1)
   mat Cdiag[3,3]=e(N2)
   qui test [IMFnn]ivlang
   mat Cdiag[5,3]=r(chi2)
   qui reg ba2tot xbis_trend3 $Z1 _Icid* _Iyear* 
   mat Cdiag[2,3]=e(r2_a)
   qui reg IMFnn ivlang imfav lnliq $Z1 _Icid* _Iyear* if year>=2000
   mat Cdiag[4,3]=e(r2_a)
   
  ** Table 2 ** 
  estout B1*, drop(_cons _I*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  
  mat li Cdiag
  di 3.85*0.44
  

* Robustness tests 
*****************************************************************
  
* Program onset 

  g IMFon=IMFnn
  replace IMFon=. if l.IMFnn==1 & IMFnn==1

  qui meprobit IMFon xbis_trend3 l.IMFon xbis_flow _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r11
   mat Diag1[1,1]=e(N)
   qui probit IMFon l.IMFon xbis_trend3 xbis_flow _Iyear*
   mat Diag1[2,1]=e(r2_p)
  qui meprobit IMFon xbis_trend3 l.IMFon $Z xbis_flow $m_z _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r12
   mat Diag1[1,2]=e(N)
   qui probit IMFon xbis_trend3 l.IMFon $Z xbis_flow $m_z _Iyear* 
   mat Diag1[2,2]=e(r2_p)
  qui meprobit IMFon xbis_trend3 l.IMFon $Z1 xbis_flow $m_z1 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r13 
   mat Diag1[1,3]=e(N)
   qui probit IMFon xbis_trend3 l.IMFon xbis_flow $Z1 $m_z  _Iyear* 
   mat Diag1[2,3]=e(r2_p)
  
  ** Table A2 **    
  estout r1*, drop(_* oL.*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  
  mat li Diag1


* World Bank financial sinks 

  qui meprobit IMFnn l.xbis_trend5 l.IMFnn xbis_flow _m_xbis_trend5 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r21
   mat Diag1[1,1]=e(N)
   qui probit IMFnn l.IMFnn xbis_trend5 xbis_flow _m_xbis_trend5 _Iyear*
   mat Diag1[2,1]=e(r2_p)
  qui meprobit IMFnn l.xbis_trend5 l.IMFnn $Z xbis_flow $m_z _m_xbis_trend5 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r22
   mat Diag1[1,2]=e(N)
   qui probit IMFnn xbis_trend5 l.IMFnn $Z xbis_flow $m_z _m_xbis_trend5 _Iyear* 
   mat Diag1[2,2]=e(r2_p)
  qui meprobit IMFnn l.xbis_trend5 l.IMFnn $Z1 xbis_flow $m_z1 _m_xbis_trend5 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r23 
   mat Diag1[1,3]=e(N)
   qui probit IMFnn xbis_trend5 l.IMFnn xbis_flow $Z1 $m_z _m_xbis_trend5 _Iyear* 
   mat Diag1[2,3]=e(r2_p)
  
  ** Table A3 **    
  estout r2*, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  
  mat li Diag1


* Alternative definitions of OFCs

  gen xbis_ch=bis_CHE/pwt_rgdp
  lab var xbis_ch "BIS/GDP flow to CHE"
  qui egen avch=mean(xbis_ch), by(cid)
  qui g xbis_trendch=xbis_ch-avch
  lab var xbis_trendch "De-trended BIS/GDP flow to CHE"
  
  qui meprobit IMFnn xbis_trendch l.IMFnn $Z xbis_taxh $m_z _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r31
   mat Diag1[1,2]=e(N)
   qui probit IMFnn xbis_trendch l.IMFnn $Z xbis_taxh $m_z _Iyear* 
   mat Diag1[2,2]=e(r2_p)
  qui meprobit IMFnn l.IMFnn bis_dev1 $Z xbis_flow $m_z _m_bis_dev1 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r32
   mat Diag1[1,3]=e(N)
   qui probit IMFnn l.IMFnn bis_dev1 $Z xbis_flow $m_z _m_bis_dev1 _Iyear* 
   mat Diag1[2,3]=e(r2_p)
   
  ** Table A4 ** 
  estout r3*, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  
  mat li Diag1


* Linear probability model 

  mat Diag=J(5,3,0)
  qui cmp (IMFnn=xbis_trend3 l.IMFnn _Icid* _Iyear*), indicators($cmp_cont) ro cl(cid) iterate(35)
   est store r41
   mat Diag[1,1]=e(N)
   qui xtreg IMFnn l.IMFnn xbis_trend3 _Iyear*, fe
   mat Diag[2,1]=e(r2_w)
  qui cmp (IMFnn=l.IMFnn xbis_trend3 $Z _Icid* _Iyear*), indicators($cmp_cont) ro cl(cid) iterate(35)
   est store r42
   mat Diag[1,2]=e(N)
   qui xtreg IMFnn l.IMFnn xbis_trend3 $Z _Iyear*, fe
   mat Diag[2,2]=e(r2_w)
  qui cmp (IMFnn=l.IMFnn xbis_trend3 $Z1 _Icid* _Iyear*), indicators($cmp_cont) ro cl(cid) iterate(35)
   est store r43
   mat Diag[1,3]=e(N)
   qui xtreg IMFnn l.IMFnn xbis_trend3 $Z1 _Iyear*, fe
   mat Diag[2,3]=e(r2_w)
   
  qui cmp (IMFnn=bis_plus1 l.IMFnn _Icid* _Iyear*), indicators($cmp_cont) ro cl(cid) iterate(35)
   est store r44
   mat Diag[4,1]=e(N)
   qui xtreg IMFnn l.IMFnn bis_plus1 _Iyear*, fe
   mat Diag[5,1]=e(r2_w)
  qui cmp (IMFnn=l.IMFnn bis_plus1 $Z _Icid* _Iyear*), indicators($cmp_cont) ro cl(cid) iterate(35)
   est store r45
   mat Diag[4,2]=e(N)
   qui xtreg IMFnn l.IMFnn bis_plus1 $Z _Iyear*, fe
   mat Diag[5,2]=e(r2_w)
  qui cmp (IMFnn=l.IMFnn bis_plus1 $Z1 _Icid* _Iyear*), indicators($cmp_cont) ro cl(cid) iterate(35)
   est store r46
   mat Diag[4,3]=e(N)
   qui xtreg IMFnn l.IMFnn bis_plus1 $Z1 _Iyear*, fe
   mat Diag[5,3]=e(r2_w)
  
  ** Table A5 **
  estout r41 r42 r43, drop(_cons _I*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  
  estout r44 r45 r46, drop(_cons _I*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  
  mat li Diag
  

* Scope condition: Institutional quality   

  su icrg_qog, d
  qui meprobit IMFnn l.xbis_trend3 l.IMFnn $Z xbis_flow $m_z _m_xbis_trend3 _Iyear* if icrg_qog<0.463 || _all:R.cid, vce(cl cid) cov(unstruc)
   est store s11
   qui probit IMFnn l.xbis_trend3 l.IMFnn $Z xbis_flow $m_z _m_xbis_trend3 _Iyear* if icrg_qog<0.463 
   mat Diag[1,1]=e(N)
   mat Diag[2,1]=e(r2_p)
  qui meprobit IMFnn l.xbis_trend3 l.IMFnn $Z xbis_flow $m_z _m_xbis_trend3 _Iyear* if icrg_qog>0.463 || _all:R.cid, vce(cl cid) cov(unstruc)
   est store s12
   qui probit IMFnn l.xbis_trend3 l.IMFnn $Z xbis_flow $m_z _m_xbis_trend3 _Iyear* if icrg_qog>0.463 
   mat Diag[2,1]=e(N)
   mat Diag[2,2]=e(r2_p)
   
  ** Table A6 **
  estout s1*, keep(L.xbis_trend3) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  
  mat li Diag
  
   
* Scope condition: Natural resource wealth 

  su natres, d
  local zsplit=`r(p50)'
  qui meprobit IMFnn xbis_trend3 l.IMFnn $Z _m_xbis_trend3 _Iyear* if natres<=`zsplit' & incid!=1 || _all:R.cid, vce(cl cid) iterate(40)
   est store s21
   qui probit IMFnn xbis_trend3 l.IMFnn $Z xbis_flow $m_z _m_xbis_trend3 _Iyear* if natres<=`zsplit' & incid!=1
   mat Diag[1,1]=e(N)
   mat Diag[2,1]=e(r2_p)
  qui meprobit IMFnn xbis_trend3 l.IMFnn $Z _m_xbis_trend3 _Iyear* if natres>=`zsplit' & incid!=1 || _all:R.cid, vce(cl cid) cov(unstruc)
   est store s22
   qui probit IMFnn xbis_trend3 l.IMFnn $Z xbis_flow $m_z _m_xbis_trend3 _Iyear* if natres>=`zsplit' & incid!=1
   mat Diag[2,1]=e(N)
   mat Diag[2,2]=e(r2_p)
   
  ** Table A7 **
  estout s2*, keep(xbis_trend3) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  
  mat li Diag
  

* Scope condition: Twilight of regime

  g x1=l2.xbis_trend3
  g x2=solchange
  g x3=x1*x2
  qui meprobit IMFnn x1 x2 x3 $Z xbis_taxh _Iyear* || _all:R.cid, vce(cl cid) iterate(40)
  grinter x1, inter(x3) const(x2) depvar(IMFnn) eq(IMFnn) clevel(90) scheme(s1mono) yline(0) nomeantext xtitle(Leader support coalition change) ytitle(Marginal effect of capital flight)
 
  
* Excess deposit alternative measure 
  
  qui meprobit IMFnn l.IMFnn bis_plus1 xbis_flow _m_bis_plus1 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r51
   mat Diag1[1,1]=e(N)
   qui probit IMFnn l.IMFnn bis_plus1 xbis_flow _m_bis_plus1 _Iyear* 
   mat Diag1[2,1]=e(r2_p) 
  qui meprobit IMFnn l.IMFnn bis_plus1 $Z xbis_flow $m_z _m_bis_plus1 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r52
   mat Diag1[1,2]=e(N)
   qui probit IMFnn l.IMFnn bis_plus1 $Z xbis_flow $m_z _m_bis_plus1 _Iyear* 
   mat Diag1[2,2]=e(r2_p)
  qui meprobit IMFnn l.IMFnn bis_plus1 $Z1 xbis_flow $m_z1 _m_bis_plus1 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r53 
   mat Diag1[1,3]=e(N)
   qui probit IMFnn l.IMFnn bis_plus1 $Z1 xbis_flow $m_z _m_bis_plus1 _Iyear* 
   mat Diag1[2,3]=e(r2_p)
   
  ** Table A8 **
  estout r5*, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3)))) order(*plus*) 
  mat li Diag1
  
  qui probit IMFnn bis_plus1 lIMFnn $Z xbis_flow $m_z _m_bis_plus1 _Iyear_2001-_Iyear_2018, vce(cl cid) 
  mfx, at(bis_plus1=0)
  * 24.2% *
  mfx, at(bis_plus1=1)
  * 40.6% *
  
  
* Differenced measures 

  mat Diag=J(5,3,0)
  qui meprobit IMFnn d.xbis_trend3 l.xbis_trend3 l.IMFnn _Iyear* _m_xbis_trend3 || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r61
   mat Diag[1,1]=e(N)
   qui probit IMFnn l.IMFnn d.xbis_trend3 l.xbis_trend3 _Iyear* _m_xbis_trend3
   mat Diag[2,1]=e(r2_p)
  qui meprobit IMFnn l.IMFnn d.xbis_trend3 l.xbis_trend3 $Z _Iyear* $m_z _m_xbis_trend3 || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r62 
   mat Diag[1,2]=e(N)
   qui probit IMFnn l.IMFnn d.xbis_trend3 l.xbis_trend3 $Z _Iyear* _m_xbis_trend3
   mat Diag[2,2]=e(r2_p)
  qui meprobit IMFnn l.IMFnn d.xbis_trend3 l.xbis_trend3 $Z1 _Iyear* $m_z1 _m_xbis_trend3 || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r63
   mat Diag[1,3]=e(N)
   qui probit IMFnn l.IMFnn d.xbis_trend3 l.xbis_trend3 $Z1 _Iyear* _m_xbis_trend3
   mat Diag[2,3]=e(r2_p)
   
  qui meprobit IMFnn d.bis_plus1 l.bis_plus1 l.IMFnn _Iyear* _m_bis_plus1 || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r64
   mat Diag[4,1]=e(N)
   qui probit IMFnn l.IMFnn d.bis_plus1 l.bis_plus1 _Iyear* _m_bis_plus
   mat Diag[5,1]=e(r2_p)
  qui meprobit IMFnn l.IMFnn d.bis_plus1 l.bis_plus1 $Z _Iyear* $m_z _m_bis_plus1 || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r65
   mat Diag[4,2]=e(N)
   qui probit IMFnn l.IMFnn d.bis_plus1 l.bis_plus1 $Z _Iyear* _m_bis_plus
   mat Diag[5,2]=e(r2_p)
   qui meprobit IMFnn l.IMFnn d.bis_plus1 l.bis_plus1 $Z1 _Iyear* $m_z1 _m_bis_plus1 || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r66
   mat Diag[4,3]=e(N)
   qui probit IMFnn l.IMFnn d.bis_plus1 l.bis_plus1 $Z1 _Iyear* _m_bis_plus
   mat Diag[5,3]=e(r2_p)

  ** Table A9 ** 
  estout r61 r62 r63, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  
  estout r64 r65 r66, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  
  mat li Diag
  

* Comprehensively defined excess deposits measure

  g FDIFLOWS=fdiflows*1e6
  
  corr exp FDIFLOWS refasy
  factor exp FDIFLOWS refasy
  factor exp FDIFLOWS refasy, fa(1)
  predict y2,
  
  qui xtreg bis_flow y2 _Iyear*, fe 
   predict y2hat, xb
   g bis_dev2=bis_flow-y2hat
   g bis_plus2=bis_dev2>0
   replace bis_plus2=. if bis_dev2==.
   su bis_plus2 bis_plus1
   corr bis_plus*
   
   egen _m_bis_plus2=mean(bis_plus2), by(cid)
  
  qui meprobit IMFnn l.IMFnn bis_plus2 xbis_flow _m_bis_plus2 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r71
   mat Diag1[1,1]=e(N)
   qui probit IMFnn l.IMFnn bis_plus2 xbis_flow _m_bis_plus2 _Iyear* 
   mat Diag1[2,1]=e(r2_p) 
  qui meprobit IMFnn l.IMFnn bis_plus2 $Z xbis_flow $m_z _m_bis_plus2 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r72
   mat Diag1[1,2]=e(N)
   qui probit IMFnn l.IMFnn bis_plus2 $Z xbis_flow $m_z _m_bis_plus2 _Iyear* 
   mat Diag1[2,2]=e(r2_p)
  qui meprobit IMFnn l.IMFnn bis_plus2 $Z1 xbis_flow $m_z1 _m_bis_plus2 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r73 
   mat Diag1[1,3]=e(N)
   qui probit IMFnn l.IMFnn bis_plus2 $Z1 xbis_flow $m_z _m_bis_plus2 _Iyear* 
   mat Diag1[2,3]=e(r2_p) 
   
  ** Table A10 **
  estout r7*, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3)))) order(*plus*) 
  mat li Diag1
    
  
* Quarterly data 

preserve

  use "Quarterly data", clear
  
  foreach x in xbis_taxh xbis3dev {
   replace `x'=`x'/100
   }
  
  sort iso3 year q
  encode time, g(timeid)
  encode iso3, g(ccode)
  
  xtset ccode timeid
  
  egen ct=group(ccode timeid)
  qui xi:reg IMFnn i.year
  sort ccode timeid

  g lngdppc=ln(wdi_gdpcapcon)
  g dcpi=d.infl_cpi
  g dlncpi=asinh(dcpi)
  egen demu=mean(demo), by(iso3)
  g chga=demo
  replace chga=demu if chga==.
  g coupi=coup_any
  replace coupi=0 if coup_any==.
  g lnref=ln(1+refasy)
  
  global Z l4.lngdppc l4.dlncpi l4.reserves_WDI l4.crisis_LV 
  global Z1 $Z l4.chga l4.coupi l4.s_unga3g7 l4.lnref
 
  qui xtreg IMFprog l.IMFprog l.xbis3dev _Iyear*, fe cl(ccode)
  est store qa1
  qui xtreg IMFprog l.IMFprog l.xbis3dev $Z _Iyear*, fe cl(ccode)
  est store qa2
  qui xtreg IMFprog l.IMFprog l.xbis3dev $Z1 _Iyear*, fe cl(ccode)
  est store qa3
  
  qui xtreg IMFprog l.IMFprog l(1/4).xbis3dev _Iyear*, fe cl(ccode)
  est store qb1
  qui xtreg IMFprog l.IMFprog l(1/4).xbis3dev $Z _Iyear*, fe cl(ccode)
  est store qb2
  qui xtreg IMFprog l.IMFprog l(1/4).xbis3dev $Z1 _Iyear*, fe cl(ccode)
  est store qb3
  
  ** Table A11 **
  estout qa*, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3)))) stats(N r2_w, fmt(0 3))  
  estout qb*, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3)))) stats(N r2_w, fmt(0 3))  

restore
  

* GFI data 

  qui meprobit IMFnn l.IMFnn gap_tr_all xbis_flow _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r81
   mat Diag1[1,1]=e(N)
   qui probit IMFnn l.IMFnn gap_tr_all xbis_flow _Iyear* 
   mat Diag1[2,1]=e(r2_p) 
  qui meprobit IMFnn l.IMFnn gap_tr_all $Z xbis_flow $m_z _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r82
   mat Diag1[1,2]=e(N)
   qui probit IMFnn l.IMFnn gap_tr_all $Z xbis_flow $m_z _Iyear* 
   mat Diag1[2,2]=e(r2_p)
  qui meprobit IMFnn l.IMFnn gap_tr_all $Z1 xbis_flow $m_z _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r83 
   mat Diag1[1,3]=e(N)
   qui probit IMFnn l.IMFnn gap_tr_all $Z1 xbis_flow $m_z _Iyear* 
   mat Diag1[2,3]=e(r2_p)
   
  ** Table A12 ** 
  estout r8*, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3)))) order(gap*)
  mat li Diag1 
  

  qui meprobit IMFnn l.IMFnn xrealkf_trend _m_realkf _Iyear* if v2x_cor>0.3 || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r91
   mat Diag1[1,1]=e(N)
   qui probit IMFnn l.IMFnn xrealkf_trend _m_realkf _Iyear* if v2x_cor>0.3
   mat Diag1[2,1]=e(r2_p) 
  qui meprobit IMFnn l.IMFnn xrealkf_trend $Z _m_realkf $m_z _Iyear* if v2x_cor>0.3 || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r92
   mat Diag1[1,2]=e(N)
   qui probit IMFnn l.IMFnn xrealkf_trend $Z _m_realkf $m_z _Iyear* if v2x_cor>0.3
   mat Diag1[2,2]=e(r2_p)
  qui meprobit IMFnn l.IMFnn xrealkf_trend $Z1 _m_realkf $m_z _Iyear* if v2x_cor>0.3 || _all:R.cid, vce(cl cid) cov(unstruc)
   est store r93 
   mat Diag1[1,3]=e(N)
   qui probit IMFnn l.IMFnn xrealkf_trend $Z1 _m_realkf $m_z _Iyear* if v2x_cor>0.3   
   mat Diag1[2,3]=e(r2_p)
   
  ** Table A13 ** 
  estout r9*, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3)))) order(*xreal*)
  mat li Diag1 
     
  
* External debt and capital flight 

  qui reghdfe xbis_taxh lndebt, a(cid year) cl(cid)
  est store m11
  qui reghdfe xbis_taxh lndebt crisis_LV, a(cid year) cl(cid)
  est store m12
  qui reghdfe xbis_taxh lndebt crisis_LV growth reserves_WDI chga coupi s_unga3 lnref, a(cid year) cl(cid)
  est store m13
  
  ** Table A14 **
  estout m1*, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3)))) stats(N r2_a, fmt(0 3))
  

* Currency crisis and NPLs

  qui reghdfe rr_curc xbis_taxh xbis_flow lngdp crd_prv if IMFnn==1, a(cid year) cl(cid)
  est store m21
  qui reghdfe rr_curc xbis_taxh xbis_flow lngdp crd_prv if IMFnn==0, a(cid year) cl(cid)
  est store m22
  qui reghdfe npl xbis_taxh xbis_flow lngdp crd_prv if IMFnn==1, a(cid year) cl(cid)
  est store m23
  qui reghdfe npl xbis_taxh xbis_flow lngdp crd_prv if IMFnn==0, a(cid year) cl(cid)
  est store m24
  
  ** Table A15 **
  estout m2*, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3)))) stats(N r2_a, fmt(0 3))
  

* Reinhart-Rogoff data 

  egen rr_crisis=rowmax(rr_bank rr_sys rr_cur rr_infl)
  replace rr_crisis=1 if rr_crisis>=1 & rr_crisis!=.
  replace rr_crisis=0 if rr_crisis==.
  
  qui reghdfe IMFnn xbis_trend3 xbis_flow rr_crisis, a(year cid) cl(cid)
  est store m31
  qui reghdfe IMFnn xbis_trend3 xbis_flow rr_crisis lngdppc dlncpi reserves_WDI chga coupi s_unga3 lnref, a(year cid) cl(cid)
  est store m32
  qui reghdfe IMFnn xbis_trend3 xbis_flow crisis_LV, a(year cid) cl(cid)
  est store m33
  qui reghdfe IMFnn xbis_trend3 xbis_flow crisis_LV lngdppc dlncpi reserves_WDI chga coupi s_unga3 lnref, a(year cid) cl(cid)
  est store m34

  ** Table A16 **
  estout m3*, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  stats(N r2_a, fmt(0 3))
  

* Crisis severity (interacted with NPLs)

  egen _m_npl=mean(npl), by(cid)  
  qui meprobit IMFnn xbis_trend3 l.IMFnn crisis_LV##c.npl_WDI reserves_WDI xbis_taxh _m_crisis _m_reserves_WDI _m_npl _m_xbis_trend3 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store m41
   qui probit IMFnn xbis_trend3 l.IMFnn crisis_LV reserves_WDI npl_WDI xbis_taxh _m_crisis _m_reserves_WDI _m_npl _m_xbis_trend3 _Iyear* 
   mat Diag[1,1]=e(N)
   mat Diag[2,1]=e(r2_p)
  qui meprobit IMFnn xbis_trend3 l.IMFnn $Z crisis#c.npl_WDI npl_WDI xbis_taxh $m_z _m_npl _m_xbis_trend3 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store m42
   qui probit IMFnn lngdppc dlncpi _m_lngdppc _m_dlncpi xbis_trend3 l.IMFnn crisis_LV reserves_WDI npl_WDI xbis_taxh _m_crisis _m_reserves_WDI _m_npl _m_xbis_trend3 _Iyear* 
    mat Diag[1,2]=e(N)
   mat Diag[2,2]=e(r2_p)
  qui meprobit IMFnn xbis_trend3 l.IMFnn $Z1 crisis#c.npl_WDI npl_WDI xbis_taxh $m_z1 _m_npl _m_xbis_trend3 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store m43
   qui probit IMFnn l.IMFnn xbis_trend3 $Z1 npl_WDI xbis_flow $m_z1 _m_npl xbis_taxh _m_xbis_trend3 _Iyear* 
   mat Diag[1,3]=e(N)
   mat Diag[2,3]=e(r2_p)
  
  ** Table A17 **
  estout m4*, drop(_* 0.*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3)))) 
  mat li Diag
    
  
* IMF programs following disasters and disaster facilities 

  g disaster=(eventsNatural>0 & totaldeaths>=25)
  g imfdisaster=0
  replace imfdisaster=1 if IMFnn==1 & (l.disaster==1 | l.imfdisaster==1)
  replace imfdisaster=. if IMFnn==.
  su imfdisaster IMFnn
  
  qui meprobit IMFnn xbis_trend3 l.IMFnn $Z _m_xbis_trend3 _Iyear* if imfdisaster!=1 || _all:R.cid, vce(cl cid) iterate(40)
   est store m51
   qui probit IMFnn xbis_trend3 l.IMFnn $Z _m_xbis_trend3 _Iyear* if imfdisaster!=1 
   mat D[1,1]=e(N)
   mat D[2,1]=e(r2_p)
  qui meprobit imfdisaster xbis_trend3 l.imfdisaster $Z _m_xbis_trend3 _Iyear* || _all:R.cid, vce(cl cid) iterate(40)
   est store m52
   qui probit imfdisaster xbis_trend3 l.imfdisas $Z _m_xbis_trend3 _Iyear* 
   mat D[1,2]=e(N)
   mat D[2,2]=e(r2_p) 
 
  g imfemf=emfcy
  replace imfemf=0 if imfemf==.
  replace imfemf=1 if l.imfemf==1 & l.month>=7 & imfemf==.
  
  qui meprobit IMFnn xbis_trend3 l.IMFnn $Z _m_xbis_trend3 _Iyear* if imfemf!=1 || _all:R.cid, vce(cl cid) iterate(40)
   est store m53
   qui probit IMFnn xbis_trend3 l.IMFnn $Z _m_xbis_trend3 _Iyear* if imfemf!=1 
   mat Diag[4,1]=e(N)
   mat Diag[5,1]=e(r2_p)
  qui meprobit imfemf xbis_trend3 l.imfemf $Z _m_xbis_trend3 _Iyear* || _all:R.cid, vce(cl cid) iterate(40)
   est store m54
   qui probit imfemf xbis_trend3 l.imfemf $Z _m_xbis_trend3 _Iyear* 
   mat Diag[4,2]=e(N)
   mat Diag[5,2]=e(r2_p)
    
 ** Table A18 **
 estout m5*, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))
 mat li Diag
 
  
* Capital flight and IMF conditions 

  qui cmp (BA2TOT=xbis_taxh xbis_flow _Icid* _Iyear*) (IMFnn=imflang xbis_flow _Icid* _Iyear*), indicators(IMFnn 1) ro cl(cid) iterate(40)
   est store m61
   qui test [IMFnn]imflang
   mat Diag[3,1]=r(chi2)
   qui reghdfe BA2TOT xbis_taxh xbis_flow if IMFnn==1, a(cid year) cl(cid)
   mat Diag[1,1]=e(r2_a)
   qui reghdfe IMFnn imflang xbis_flow, a(cid year) cl(cid)
   mat Diag[2,1]=e(r2_a)
  qui cmp (BA2TOT=xbis_taxh xbis_flow $Z _Icid* _Iyear*) (IMFnn=imflang  $Z _Icid* _Iyear*), indicators(IMFnn 1) ro cl(cid) iterate(40)
   est store m62
   qui test [IMFnn]imflang
   mat Diag[3,2]=r(chi2)
   qui reghdfe BA2TOT xbis_taxh xbis_flow $Z if IMFnn==1, a(cid year) cl(cid)
   mat Diag[1,2]=e(r2_a)
   qui reghdfe IMFnn imflang xbis_flow $Z, a(cid year) cl(cid)
   mat Diag[2,2]=e(r2_a)
  qui cmp (BA2TOT=xbis_taxh xbis_flow $Z1 _Icid* _Iyear*) (IMFnn=imflang  $Z1 _Icid* _Iyear*), indicators(IMFnn 1) ro cl(cid) iterate(40)
   est store m63
   qui test [IMFnn]imflang
   mat Diag[3,3]=r(chi2)
   qui reghdfe BA2TOT xbis_taxh xbis_flow $Z1 if IMFnn==1, a(cid year) cl(cid)
   mat Diag[1,3]=e(r2_a)
   qui reghdfe IMFnn imflang xbis_flow $Z1, a(cid year) cl(cid)
   mat Diag[2,3]=e(r2_a)

  ** Table 2 ** 
  estout m6*, drop(_cons ) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3)))) stats(N r2_a, fmt(0 3)) 
  mat li F 
  di 3.85*0.44
 

* Types of conditions

  qui reghdfe BA2FIN xbis_taxh xbis_flow $Z if IMFnn==1, a(cid year) cl(cid)
  est store m71 
  qui reghdfe BA2EXT xbis_taxh xbis_flow $Z if IMFnn==1, a(cid year) cl(cid)
  est store m72
  qui reghdfe BA2FP xbis_taxh xbis_flow $Z if IMFnn==1, a(cid year) cl(cid)
  est store m73 
  qui reghdfe BA2PRI xbis_taxh xbis_flow $Z if IMFnn==1, a(cid year) cl(cid)
  est store m74
  
  ** Table A20 **
  estout m7*, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3)))) stats(N r2_a, fmt(0 3))


* PERI measure with instrumentation

  g ivXZ=l.Xdebt*l.VIX 
  mat IVafr=J(8,3,0)
  
  qui cmp (ba2tot=xrealkf_trend $Z _Icid* _Iyear*) (xrealkf_trend=ivXZ l.VIX l.Xdebt _Icid* _Iyear*) (IMFnn=ivlang imfav lnliq $Z _Icid* _Iyear*) if regid==1|regid==5, indicators(IMFnn $cmp_cont $cmp_cont) ro cl(cid) iterate(30)
   est store m81
   mat IVafr[1,1]=e(N1)
   mat IVafr[3,1]=e(N2)
   mat IVafr[6,1]=e(N3)
   qui test [xrealkf_trend]ivXZ
   mat IVafr[5,1]=r(chi2)
   qui test [IMFnn]ivlang
   mat IVafr[8,1]=r(chi2)
   qui xtreg ba2tot ivXZ $Z _Iyear*, fe
   mat IVafr[2,1]=e(r2_w)
   qui xtreg xrealkf_trend ivXZ l.VIX l.Xdebt _Iyear*, fe
   mat IVafr[4,1]=e(r2_w)
   qui xtreg IMFnn ivlang imfav lnliq $Z _Iyear*, fe
   mat IVafr[7,1]=e(r2_w)
   
  qui cmp (ba2tot=xrealkf_trend $Z _Icid* _Iyear*) (xrealkf_trend=ivXZ l.VIX l.Xdebt _Icid* _Iyear*) (IMFnn=ivlang imfav lnliq $Z _Icid* _Iyear*) if regid==1|regid==5, indicators(IMFnn $cmp_cont $cmp_cont) ro cl(cid) iterate(30)
   est store m82
   mat IVafr[1,2]=e(N1)
   mat IVafr[3,2]=e(N2)
   mat IVafr[6,2]=e(N3)
   qui test [xrealkf_trend]ivXZ
   mat IVafr[5,2]=r(chi2)
   qui test [IMFnn]ivlang
   mat IVafr[8,2]=r(chi2)
   qui xtreg ba2tot ivXZ $Z _Iyear*, fe
   mat IVafr[2,2]=e(r2_w)
   qui xtreg xrealkf_trend ivXZ l.VIX l.Xdebt _Iyear*, fe
   mat IVafr[4,2]=e(r2_w)
   qui xtreg IMFnn ivlang imfav lnliq $Z _Iyear*, fe
   mat IVafr[7,2]=e(r2_w)
   
  qui cmp (ba2tot=xrealkf_trend $Z1 _Icid* _Iyear*) (xrealkf_trend=ivXZ l.VIX l.Xdebt _Icid* _Iyear*) (IMFnn=ivlang imfav lnliq $Z1 _Icid* _Iyear*) if regid==1|regid==5, indicators(IMFnn $cmp_cont $cmp_cont) ro cl(cid) iterate(30)
   est store m83
   mat IVafr[1,3]=e(N1)
   mat IVafr[3,3]=e(N2)
   mat IVafr[6,3]=e(N3)
   qui test [xrealkf_trend]ivXZ
   mat IVafr[5,3]=r(chi2)
   qui test [IMFnn]ivlang
   mat IVafr[8,3]=r(chi2)
   qui xtreg ba2tot ivXZ $Z _Iyear*, fe
   mat IVafr[2,3]=e(r2_w)
   qui xtreg xrealkf_trend ivXZ l.VIX l.Xdebt _Iyear*, fe
   mat IVafr[4,3]=e(r2_w)
   qui xtreg IMFnn ivlang imfav lnliq $Z _Iyear*, fe
   mat IVafr[7,3]=e(r2_w)
    
  ** Table A21 ** 	
  estout m8*, drop(_cons _I*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  
  mat li IVafr
  
  
* Elite capital flight, WB program, and IMF program

  qui meprobit wbsal l2.xbis_trend5 l.wbsal xbis_and _m_xbis_trend5 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store wb11
   mat Diag1[1,1]=e(N)
   qui probit wbsal l.wbsal l2.xbis_trend5 xbis_and _m_xbis_trend5 _Iyear*
   mat Diag1[2,1]=e(r2_p)
  qui meprobit wbsal l2.xbis_trend5 l.wbsal $Z xbis_and $m_z _m_xbis_trend5 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store wb12
   mat Diag1[1,2]=e(N)
   qui probit wbsal l2.xbis_trend5 l.wbsal $Z xbis_and $m_z _m_xbis_trend5 _Iyear* 
   mat Diag1[2,2]=e(r2_p)
  qui meprobit wbsal l2.xbis_trend5 l.wbsal $Z1 xbis_and $m_z1 _m_xbis_trend5 _Iyear* || _all:R.cid, vce(cl cid) cov(unstruc)
   est store wb13 
   mat Diag1[1,3]=e(N)
   qui probit wbsal l2.xbis_trend5 l.wbsal xbis_and $Z1 $m_z _m_xbis_trend5 _Iyear* 
   mat Diag1[2,3]=e(r2_p)

  ** Table A22 **    
  estout wb1*, drop(_*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  
  mat li Diag1
  

* Elite capital flight, WB program, and IMF program using instrument

  egen wbav=mean(wbsal), by(cid)
  qui g ivwb=wbav*lnliq
  mat Cdiag=J(5,3,0)
 
  qui cmp (count=l2.xbis_trend3 xbis_flow _Icid* _Iyear*) (wbsal=ivwb wbav lnliq xbis_flow _Icid* _Iyear*) if year>=2000, indicators(wbsal $cmp_cont) ro cl(cid) iterate(35)
   est store wb21
   mat Cdiag[1,1]=e(N1)
   mat Cdiag[3,1]=e(N2)
   qui test [wbsal]ivwb
   mat Cdiag[5,1]=r(chi2)
   qui reg count l2.xbis_trend3 _Icid* _Iyear* 
   mat Cdiag[2,1]=e(r2_a)
   qui reg wbsal ivwb wbav lnliq _Icid* _Iyear* if year>=2000
   mat Cdiag[4,1]=e(r2_a)
  qui cmp (count=l2.xbis_trend3 $Z xbis_flow _Icid* _Iyear*) (wbsal=ivwb wbav lnliq $Z xbis_flow _Icid* _Iyear*) if year>=2000, indicators(wbsal $cmp_cont) ro cl(cid) iterate(35)
   est store wb22
   mat Cdiag[1,2]=e(N1)
   mat Cdiag[3,2]=e(N2)
   qui test [wbsal]ivwb
   mat Cdiag[5,2]=r(chi2)
   qui reg count l2.xbis_trend3 $Z xbis_flow _Icid* _Iyear* 
   mat Cdiag[2,2]=e(r2_a)
   qui reg wbsal ivwb wbav $Z xbis_flow lnliq _Icid* _Iyear* if year>=2000
   mat Cdiag[4,2]=e(r2_a)
  qui cmp (count=l2.xbis_trend3 $Z1 xbis_flow _Icid* _Iyear*) (wbsal=ivwb wbav lnliq xbis_flow $Z1 _Icid* _Iyear*) if year>=2000, indicators(wbsal $cmp_cont) ro cl(cid) iterate(35)
   est store wb23 
   mat Cdiag[1,3]=e(N1)
   mat Cdiag[3,3]=e(N2)
   qui test [wbsal]ivwb
   mat Cdiag[5,3]=r(chi2)
   qui reg count l2.xbis_trend3 $Z1 xbis_flow _Icid* _Iyear* 
   mat Cdiag[2,3]=e(r2_a)
   qui reg wbsal ivwb wbav lnliq $Z1 xbis_flow _Icid* _Iyear* if year>=2000
   mat Cdiag[4,3]=e(r2_a)
   
  ** Table A23 ** 
  estout wb2*, drop(_cons _I*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  
  mat li Cdiag
  

* Elite capital flight, WB program, and IMF program using instrument and selection

  mat Ddiag=J(8,3,0)
  g livXZ=l2.Xdebt*l2.VIX
  set matsize 800
  
  qui cmp (count=l.xbis_trend3 l.xbis_flow _Icid* _Iyear*) (l.xbis_trend3=livXZ l2.VIX l2.Xdebt _Icid* _Iyear*) (wbsal=ivwb wbav lnliq _Icid* _Iyear*) if year>=2000, indicators(wbsal $cmp_cont $cmp_cont) ro cl(cid) iterate(35) difficult
   est store wb31
   mat Ddiag[1,1]=e(N1)
   mat Ddiag[3,1]=e(N2)
   mat Ddiag[6,1]=e(N3)
   qui test [wbsal]ivwb
   mat Ddiag[8,1]=r(chi2)
   qui test [lxbis_trend3]livXZ
   mat Ddiag[5,1]=r(chi2)
   qui reg l.xbis_trend3 l.xbis_flow livXZ l2.VIX l2.Xdebt _Icid* _Iyear* 
   mat Ddiag[4,1]=e(r2_a)
   qui reg count l.xbis_trend3 _Icid* _Iyear* 
   mat Ddiag[2,1]=e(r2_a)
   qui reg wbsal ivwb wbav lnliq _Icid* _Iyear* if year>=2000
   mat Ddiag[7,1]=e(r2_a) 
  
  qui cmp (count=l.xbis_trend3 l.xbis_flow $Z _Icid* _Iyear*) (l.xbis_trend3=livXZ l2.VIX l2.Xdebt $Z _Icid* _Iyear*) (wbsal=ivwb wbav lnliq $Z _Icid* _Iyear*) if year>=2000, indicators(wbsal $cmp_cont $cmp_cont) ro cl(cid) iterate(35) difficult
   est store wb32
   mat Ddiag[1,2]=e(N1)
   mat Ddiag[3,2]=e(N2)
   mat Ddiag[6,2]=e(N3)
   qui test [wbsal]ivwb
   mat Ddiag[8,2]=r(chi2)
   qui test [lxbis_trend3]livXZ
   mat Ddiag[5,2]=r(chi2)
   qui reg l.xbis_trend3 l.xbis_flow livXZ l2.VIX l2.Xdebt $Z _Icid* _Iyear* 
   mat Ddiag[4,2]=e(r2_a)
   qui reg count l.xbis_trend3 $Z _Icid* _Iyear* 
   mat Ddiag[2,2]=e(r2_a)
   qui reg wbsal ivwb wbav lnliq $Z _Icid* _Iyear* if year>=2000
   mat Ddiag[7,2]=e(r2_a)
   
  qui cmp (count=l.xbis_trend3 l.xbis_flow $Z1 _Icid* _Iyear*) (l.xbis_trend3=livXZ l2.VIX l2.Xdebt $Z1 _Icid* _Iyear*) (wbsal=ivwb wbav lnliq $Z1 _Icid* _Iyear*) if year>=2000, indicators(wbsal $cmp_cont $cmp_cont) ro cl(cid) iterate(35) difficult
   est store wb33
   mat Ddiag[1,3]=e(N1)
   mat Ddiag[3,3]=e(N2)
   mat Ddiag[6,3]=e(N3)
   qui test [wbsal]ivwb
   mat Ddiag[8,3]=r(chi2)
   qui test [lxbis_trend3]livXZ
   mat Ddiag[5,3]=r(chi2)
   qui reg l.xbis_trend3 livXZ l2.VIX l2.Xdebt $Z1 _Icid* _Iyear* 
   mat Ddiag[4,3]=e(r2_a)
   qui reg count l.xbis_trend3 $Z1 _Icid* _Iyear* 
   mat Ddiag[2,3]=e(r2_a)
   qui reg wbsal ivwb wbav lnliq $Z1 _Icid* _Iyear* if year>=2000
   mat Ddiag[7,3]=e(r2_a)
  
  ** Table A24 ** 
  estout wb3*, drop(_cons _I*) starlevels(* .1 ** .05 *** .01) cells((b(star fmt(3)) se(par fmt(3))))  
  mat li Ddiag
  

* Descriptive statistics 

  qui estpost su IMFnn BA2TOT xbis_trend3 xbis_trend4 xbis_trend5 xbis_trendch bis_plus1 gap_tr_all xrealkf_trend $Z1 xbis_flow VIX Xdebt KOFFiGIdf kc_out kc_in  icrg_qog natres imfdisaster imfemf intr_us imf_liq
  
  ** Table A1 **
  esttab ., cells("count mean sd min max") noobs
  